const Merge = require("../template")

function testMergeAdvancedMethods() {
  console.log(
    "1. 类型检查合并测试:",
    (() => {
      const target = { a: 1, b: 'hello' }
      const source = { a: 'world', b: 2 }
      const result = Merge.mergeWithTypeCheck(target, source, true)
      const expected = { a: 1, b: 'hello' } // 类型不匹配，保持原值
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 默认值合并测试:",
    (() => {
      const target = { a: 1 }
      const source = { b: 2 }
      const defaults = { a: 0, b: 0, c: 0 }
      const result = Merge.mergeWithDefaults(target, source, defaults)
      const expected = { a: 1, b: 2, c: 0 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 路径合并测试:",
    (() => {
      const target = { user: { name: 'John', age: 30 } }
      const source = { user: { age: 31, city: 'NYC' } }
      const result = Merge.mergeAtPath(target, source, 'user')
      const expected = { user: { name: 'John', age: 31, city: 'NYC' } }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "4. 验证合并测试:",
    (() => {
      const target = { name: 'John', age: 30 }
      const source = { age: 31, email: 'john@example.com' }
      const schema = {
        name: 'string',
        age: 'number',
        email: 'string'
      }
      const result = Merge.mergeWithValidation(target, source, schema)
      const expected = { name: 'John', age: 31, email: 'john@example.com' }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 转换合并测试:",
    (() => {
      const target = { name: 'john', age: '30' }
      const source = { age: '31', city: 'nyc' }
      const transformers = {
        name: (value) => value.toUpperCase(),
        city: (value) => value.toUpperCase()
      }
      const result = Merge.mergeWithTransform(target, source, transformers)
      const expected = { name: 'JOHN', age: '31', city: 'NYC' }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

    // 处理异步测试的结果显示
    (async () => {
      console.log(
        "6. 异步合并测试:",
        await (() => {
          const target = { a: 1, b: 2 }
          const source = { b: 3, c: 4 }
          const processor = async (value) => value * 2

          return new Promise((resolve) => {
            Merge.mergeAsync(target, source, processor).then((result) => {
              const expected = { a: 2, b: 6, c: 8 }
              resolve(JSON.stringify(result) === JSON.stringify(expected))
            })
          })
        })()
      )

      console.log(
        "7. 并发合并测试:",
        await (() => {
          const target = { a: 1, b: 2 }
          const source = { b: 3, c: 4 }
          const processor = async (value) => value * 2

          return new Promise((resolve) => {
            Merge.mergeConcurrent(target, source, processor, 2).then((result) => {
              const expected = { a: 2, b: 6, c: 8 }
              resolve(JSON.stringify(result) === JSON.stringify(expected))
            })
          })
        })()
      )
    })()

  console.log(
    "8. 复杂对象合并测试:",
    (() => {
      const target = {
        user: {
          profile: { name: 'John', age: 30 },
          settings: { theme: 'dark', lang: 'en' }
        },
        data: [1, 2, 3]
      }
      const source = {
        user: {
          profile: { age: 31, city: 'NYC' },
          settings: { lang: 'zh' }
        },
        data: [4, 5, 6]
      }
      const result = Merge.merge(target, source)

      return result.user.profile.name === 'John' &&
        result.user.profile.age === 31 &&
        result.user.profile.city === 'NYC' &&
        result.user.settings.theme === 'dark' &&
        result.user.settings.lang === 'zh' &&
        JSON.stringify(result.data) === JSON.stringify([4, 5, 6])
    })()
  )

  console.log(
    "9. 数组去重合并测试:",
    (() => {
      const target = { items: [1, 2, 3] }
      const source = { items: [2, 3, 4] }
      const result = Merge.mergeWithArrayStrategy(target, source, 'unique')
      const expected = { items: [1, 2, 3, 4] }
      return JSON.stringify(result.items.sort()) === JSON.stringify(expected.items.sort())
    })()
  )

  console.log(
    "10. 数组连接合并测试:",
    (() => {
      const target = { items: [1, 2] }
      const source = { items: [3, 4] }
      const result = Merge.mergeWithArrayStrategy(target, source, 'concat')
      const expected = { items: [1, 2, 3, 4] }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "11. this上下文高级方法测试:",
    (() => {
      const advancedObj = {
        multiplier: 2,
        processAdvanced: function(target, source) {
          const merged = Merge.mergeWithTransform(target, source, {
            value: (val) => val * this.multiplier
          })
          return merged.value
        }
      }
      const target = { value: 5 }
      const source = { value: 3 }
      return advancedObj.processAdvanced(target, source) === 6
    })()
  )

  console.log(
    "12. 复杂对象this绑定高级方法测试:",
    (() => {
      const advancedConfigObj = {
        config: { threshold: 5 },
        processWithConfig: function(target, source) {
          const merged = Merge.mergeWithTransform(target, source, {
            value: (val) => val * 2
          })
          return merged.value >= this.config.threshold
        }
      }
      const target = { value: 2 }
      const source = { value: 3 }
      return advancedConfigObj.processWithConfig(target, source) === true
    })()
  )
}

testMergeAdvancedMethods()

